package bankapplication.com.acme.bankapp.domain.bank;

public class EmailQueueParser implements Runnable{

  private final Queue<Email> emailqueue;
  
  public EmailQueueParser(Queue<Email> emailqueue){
    this.emailqueue = emailqueue;
  }
  private void sendEmail(Email email){
    System.out.println("Sending email to client : " + email.getClient().toString());
  }
  
  public void run(){
    while(!Thread.interrupted()){
      synchronized(emailqueue){
        Email email = emailqueue.get();
        if(email != null){
          sendEmail(email);
          emailqueue.remove();
        }
        else{
          try{
            emailqueue.wait();
          }
          catch(InterruptedException e){
            return;
          }
        }
      }
    }
  }
}
